(function (G) {
    const javaUrl = G.Storage.get('JAVAURL')
    G.vue({
        data: {
            address: '', // 当前地址
            latitude: '', // 当前纬度
            longitude: '', // 当前经度
            img:'',
            picList: [],
            APIURL: G.Storage.get('APIURL'),
            currentTime:'',
            top:wx.getSystemInfoSync().statusBarHeight + wx.getMenuButtonBoundingClientRect().top
        },
        methods: {
            //返回上一级
            goback(){
                wx.reLaunch({
                    url: '/pages/index/index',
                })   
                G.Storage.set('takePicMode','')      
            },
            // 获取当前定位
            getlocal() {
                // wx.showLoading({
                //     title: '加载中',
                // })
                let that = this            
                // console.log('userinfo', G.Storage.get('userInfo'));
                that.setData({
                    userId1: G.Storage.get('userInfo').userName
                })
                wx.getLocation({
                    type: 'gcj02', //返回可以用于 wx.openLocation 的经纬度
                    success(res) {
                        ///真实获取经纬度
                        const latitude = res.latitude
                        const longitude = res.longitude
                        that.setData({
                            latitude, 
                            longitude,
                        })
                        ///结束
                        console.log('--to getLoctionInfo',latitude,longitude);
                        that.getLoctionInfo()
                        if(!that.data.timer){
                            that.data.timer = setInterval(()=>{
                                that.getlocal()
                            },1000 * 5)
                        }
                    },
                    fail(e) {
                        //console.log('--e', e);
                    }
                })
                if (that.data.timeController) {
                    setTimeout(() => {
                        that.getlocal()
                    }, 10000)
                }
            },
            // 传入经纬度获取该点信息
            getLoctionInfo() {
                G.get('/v1/map/geocodeRegeo', {
                    longitude: this.data.longitude,
                    latitude: this.data.latitude
                }).then(res => {
                    this.setData({
                        address: res.regeocode.formatted_address
                    })
                })
            }, 
            // 拍照
            upload(e){
                if(this.data.adding){
                    return false
                }
                this.data.adding = true
                G.postWeRun().then(res => {
                    this.data.adding = false
                    if(res.errorMsg) return false
                    let _self = this;
                    wx.chooseMedia({
                        count: 1,
                        mediaType: ['image'],
                        sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
                        sourceType: ['camera'], //从相册选择
                        success(res) {
                            console.log(res, '选择图片')
                            _self.chooseMediaa(res.tempFiles[0].tempFilePath)
                        },
                        error: function (e) {
                            console.log(e);
                        }
                    });
                })
            },
            upload1(e) {
                if(this.data.adding){
                    return false
                }
                this.data.adding = true
                G.postWeRun().then(res => {
                    this.data.adding = false
                    if(res.errorMsg) return false
                    const {
                        type
                    } = e.currentTarget.dataset
                    let _self = this;
                    const ctx = wx.createCameraContext()
                    ctx.takePhoto({
                        quality: 'high',
                        success: (res) => {
                            _self.chooseMediaa(res.tempImagePath)
                        }
                    })
                })
            },
            uploadRequest(img) {
                let _self = this;
                wx.showLoading({
                    title: '上传中...',
                })
                wx.uploadFile({
                    url: G.Storage.get('APIURL') + '/upload',
                    filePath: img,
                    name: 'file',
                    header: {
                        'token': G.Storage.get('userInfo').token
                    },
                    success(rej) {
                        try {
                            let showUrl = JSON.parse(rej.data).id
                            let url = G.Storage.get('APIURL') + '/file/' + showUrl
                            console.log(url, 'url')
                            let array = wx.getStorageSync('imgList') || []
                            // 向数组中追加
                            array.unshift({
                              showUrl:showUrl,
                              url:url,
                              latitude:_self.data.latitude,
                              longitude:_self.data.longitude,
                              address:_self.data.address,
                              time:new Date().getTime(),
                              code:0
                            })
                            _self.setData({
                              picList:array
                            })
                            // 重新设置缓存
                            wx.setStorage({
                              key: 'imgList',
                              data: array
                            })
                            
                            wx.hideLoading()
                            // _self.chooseMedia(img)
                        } catch (e) {
                            //TODO handle the exception
                        }
                    },
                    fail(err) {
                        console.log(err)
                    }
                });
            },
            chooseMediaa(tempImagePath) {
                let that = this
                let list = that.data.img || []
                wx.uploadFile({
                    url: G.Storage.get('APIURL') + '/upload',
                    filePath: tempImagePath,
                    name: 'file',
                    header: {
                        'token': G.Storage.get('userInfo').token
                    },
                    success(rej) {
                        console.log('---re', rej);
                        let url1 = '/file/' + JSON.parse(rej.data).id
                        list.push(url1)
                        that.setData({
                            img: list
                        })
                    },
                    fail(err) {
                        console.log(err)
                    }
                });
                that.uploadRequest(tempImagePath)
            },
            // 微信图片调用upload
            uploadRequestg(img) {
                wx.uploadFile({
                    url: G.Storage.get('APIURL') + '/upload',
                    filePath: img,
                    name: 'file',
                    header: {
                        'token': G.Storage.get('userInfo').token
                    },
                    success(rej) {
                        console.log('---rej.data', rej.data);
                        return JSON.parse(rej.data).id
                    },
                    fail(err) {
                        console.log(err)
                    }
                });
            },
            // 跳转页面
            gotolist(){
                wx.navigateTo({
                  url: '/pages/takepic/list/index'
                })
            },
            // 格式化时间--时分
            formatDate(times) {
                var date = new Date(times);
                var year = date.getFullYear().toString(); //年份
                var month = (date.getMonth() + 1).toString(); //月份
                var day1 = date.getDate(); //日
                if (day1.toString().length == 1) {
                    var day = '0' + day1.toString()
                } else {
                    var day = day1.toString()
                }
                var hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
                var minute = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
                var second = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
                return month + '/' + day +  '/' + year + ' ' + hour + ':' + minute + ':' + second
                //   year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second
            },
            // 获取实时时间（时分）显示在页面
            getCurrentTime() {
                let that = this
                let time1 = new Date();
                // console.log('----time1', time1);
                let time2 = that.formatDate(time1)
                // console.log('----time2', time2);
                that.setData({
                    currentTime: new Date().getTime()
                })
                // console.log('---currentTime', that.data.currentTime);
                if (that.data.timeController) {
                    setTimeout(() => {
                        that.getCurrentTime()
                    }, 1000)
                }
            },
            onShow(){
                this.setData({
                    picList:wx.getStorageSync('imgList') || []
                })
                if(this.loaded) {
                    this.getlocal()
                }
            },
            onHide(){
                if(this.data.timer) {
                    clearInterval(this.data.timer)
                    this.data.timer = 0
                }
            },
            onUnload(){
                if(this.data.timer) {
                    clearInterval(this.data.timer)
                    this.data.timer = 0
                }
            },
            // face(){
            //     const session = wx.createVKSession({
            //         track: {
            //             face: {mode: 1},
            //         },
            //         version: 'v2'
            //       })
            //       // 逐帧分析
            //     const onFrame = timestamp => {
            //         // 开发者可以自己控制帧率
            //         const frame = session.getVKFrame(100, 100)
            //         if (frame) {
            //             // 分析完毕，可以拿到帧对象
            //             doRender(frame)
            //           }
                  
            //         // session.requestAnimationFrame(onFrame)
            //       }
            //       session.start(err => {
            //         if (!err) session.requestAnimationFrame(onFrame)
            //       })
                  
            //       // 渲染函数
            //       const doRender = frame => {
            //         // ...
            //         console.log(frame)
            //       }
                
            // }
        },
        mounted(options) {
            // this.face()
            this.getlocal()
            this.loaded = true
            // this.getCurrentTime()
            G.Storage.set('takePicMode',true)
        },
        
    })
})(wx.$)